package hgx.spring.cloud.lession.orderserver.dao;/**
 * Created by liujun on 2018/3/15.
 */

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import hgx.spring.cloud.lession.orderserver.api.IOrderRepository;
import hgx.spring.cloud.lession.orderserver.vo.OrderItemVo;
import hgx.spring.cloud.lession.orderserver.vo.OrderVo;

/**
 * @author Administrator
 * @create 2018-03-15 14:25
 */

@Repository
public class OrderDaoImpl implements IOrderRepository{

    private List<OrderVo> orderVos ;


    @Value("${order.findOrderByUserId.sleepTime}")
    private int findOrderByUserId;

    @Value("${order.findAll.sleepTime}")
    private int findAll;

    @Value("${order.getOrderVoById.sleepTime}")
    private int getOrderVoById;

    public OrderDaoImpl() {
        initData();
    }

    @Override
    public boolean createOrder(OrderVo orderVo) {
        if(orderVo == null){
            return false;
        }

        if(orderVos == null){
            orderVos=new ArrayList<>(1);
        }

        orderVo.setId((long) (orderVos.size() + 1));
        orderVos.add(orderVo);

        return true;
    }

    @Override
    public List<OrderVo> findAll() {
        try {
            TimeUnit.MILLISECONDS.sleep(findAll);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return orderVos;
    }

    @Override
    public List<OrderVo> findOrderByUserId(Long userId) {
        try {
            TimeUnit.MILLISECONDS.sleep(findOrderByUserId);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if(userId == null || orderVos == null){
            return null;
        }

        List<OrderVo> orderVoList=new ArrayList<>(5);
        for(OrderVo vo : orderVos){
            if(vo !=null && userId.equals(vo.getUserId())){
                orderVoList.add(vo);
            }
        }

        return orderVoList;
    }

    @Override
    public OrderVo getOrderVoById(Long orderNo) {
        try {
            TimeUnit.MILLISECONDS.sleep(getOrderVoById);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if(orderNo == null || orderVos == null){
            return null;
        }

        for(OrderVo vo : orderVos){
            if(vo !=null && orderNo.equals(vo.getUserId())){
                return vo;
            }
        }
        return null;
    }


    private void initData(){
        orderVos=new ArrayList<>(2);
        OrderVo orderVo=new OrderVo();
        orderVo.setCreateDate(new Date());
        orderVo.setId(1L);
        orderVo.setStatus((byte)1);
        orderVo.setTotalMoney(265.80f);
        orderVo.setUserId(1L);
        orderVo.setUpdateDate(new Date());

        OrderItemVo orderItemVo1=new OrderItemVo();
        orderItemVo1.setCreateDate(new Date());
        orderItemVo1.setId(1L);
        orderItemVo1.setNum(5);
        orderItemVo1.setPrice(15.36f);
        orderItemVo1.setProductId(77L);

        orderVo.addOrderItemVo(orderItemVo1);

        OrderItemVo orderItemVo2=new OrderItemVo();
        orderItemVo2.setCreateDate(new Date());
        orderItemVo2.setId(2L);
        orderItemVo2.setNum(3);
        orderItemVo2.setPrice(57.26f);
        orderItemVo2.setProductId(66L);

        orderVo.addOrderItemVo(orderItemVo2);

        OrderItemVo orderItemVo3=new OrderItemVo();
        orderItemVo3.setCreateDate(new Date());
        orderItemVo3.setId(3L);
        orderItemVo3.setNum(7);
        orderItemVo3.setPrice(28.36f);
        orderItemVo3.setProductId(99L);

        orderVo.addOrderItemVo(orderItemVo3);

        orderVos.add(orderVo);


        OrderVo orderVo2=new OrderVo();
        orderVo2.setCreateDate(new Date());
        orderVo2.setId(2L);
        orderVo2.setStatus((byte)3);
        orderVo2.setTotalMoney(105.3f);
        orderVo2.setUserId(1L);
        orderVo2.setUpdateDate(new Date());

        OrderItemVo orderItemVo4=new OrderItemVo();
        orderItemVo4.setCreateDate(new Date());
        orderItemVo4.setId(4L);
        orderItemVo4.setNum(7);
        orderItemVo4.setPrice(28.36f);
        orderItemVo4.setProductId(99L);

        orderVo2.addOrderItemVo(orderItemVo4);
        orderVos.add(orderVo2);


    }
}
